﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AppCommerciaux.Views
{
    /// <summary>
    /// Logique d'interaction pour UserControlAuthentification.xaml
    /// </summary>
    public partial class UserControlAuthentification : UserControl
    {
        AccueilPrincipalView apv;

        //public UserControlAuthentification()
        //{
        //    InitializeComponent();
        //}

        public UserControlAuthentification(String action)
        {
            InitializeComponent();
            if (action == "CMdP")
            {
                gridConnexion.Visibility = System.Windows.Visibility.Collapsed;
                gridInfoConnexion.Visibility = System.Windows.Visibility.Collapsed;
                gridSaisieInfos.Visibility = System.Windows.Visibility.Collapsed;
                gridChangerMdp.Visibility = System.Windows.Visibility.Visible;
            }

        }

        public UserControlAuthentification(AccueilPrincipalView apv)
        {
            InitializeComponent();
            this.apv = apv;
            textBoxLogin.Text = "toto";
            passwordBox.Password = "titi";
        }

        private void buttonConnexion_Click(object sender, RoutedEventArgs e)
        {
            if (passwordBox.Password == "a")
            {
                buttonCreation.Visibility = System.Windows.Visibility.Visible;
                gridSaisieInfos.Visibility = System.Windows.Visibility.Visible;
                textBoxLogin.Text = "";
                passwordBox.Password = "";
            }
            else
            {
                buttonCreation.Visibility = System.Windows.Visibility.Collapsed;
                gridSaisieInfos.Visibility = System.Windows.Visibility.Collapsed;
                //tempTextBlock.Visibility = System.Windows.Visibility.Collapsed;

                if (Authentification.Login(textBoxLogin.Text, passwordBox.Password, "lambda"))
                {



                    /*
                    //-> A mettre dans le footer quand Armand aura fini de modifier la fenêtre d'accueil
                    // On masque les zones de saisie de connexion
                    gridConnexion.Visibility = System.Windows.Visibility.Collapsed;

                    // => Affichage de l'écran d'accueil de l'appli
                    String[] tabInfoConnexion = Authentification.getInfoConnexion();
                    labelInfoConnexion.Content = "Bienvenue " + tabInfoConnexion[1] + " " + tabInfoConnexion[0] +
                        " ! Et bon travail sur l'appli Commerciale PlastProd !\nDernière connexion : " + tabInfoConnexion[2];
                    gridInfoConnexion.Visibility = System.Windows.Visibility.Visible;
                    */
                    String[] tabInfoConnexion = Authentification.getInfoConnexion();
                    apv.textFooter.Inlines.Clear();
                    apv.textFooter.Inlines.Add("Bienvenue " + tabInfoConnexion[1] + " " + tabInfoConnexion[0] +
                        " ! Et bon travail sur l'appli Commerciale PlastProd !\nDernière connexion : " + tabInfoConnexion[2]);

                    apv.buttonClient.IsEnabled = true;
                    apv.buttonCommande.IsEnabled = true;
                    apv.buttonDeconnexion.IsEnabled = true;
                    apv.buttonRendezVous.IsEnabled = true;
                    apv.buttonSynchronisation.IsEnabled = true;
                    apv.buttonChangerMdp.IsEnabled = true;

                    ConsulterRendezVousView windowAgenda = new ConsulterRendezVousView();
                    apv.centralGrid.Children.Clear();
                    apv.centralGrid.Children.Add(windowAgenda);

                }
                else
                {
                    MessageBox.Show("Connexion refusée !");
                    textBoxLogin.Text = "";
                    passwordBox.Password = "";
                }
            }
        }

        private void buttonCreation_Click(object sender, RoutedEventArgs e)
        {
            buttonCreation.Visibility = System.Windows.Visibility.Collapsed;
            gridSaisieInfos.Visibility = System.Windows.Visibility.Collapsed;
            if (Authentification.Create(textBoxLogin.Text, passwordBox.Password, textboxNom.Text, textboxPrenom.Text, "lambda"))
            {
                MessageBox.Show("Création de compte réussie !");
            }
            else
            {
                MessageBox.Show("Echec de création de compte !");
            }
            textBoxLogin.Text = "";
            passwordBox.Password = "";

        }

        /**
         * Validation de la saisie de changement de mot de passe
         */
        private void buttonValidation_Click(object sender, RoutedEventArgs e)
        {
            if (passwordBoxFutur1.Password == "" && passwordBoxFutur2.Password == "")
            {
                MessageBox.Show("Le futur mot de passe et celui de confirmation ne doivent pas être nuls !");
            }
            else
            {
                String message = "";
                Int16 resultat = Authentification.ChangerMotPasse(passwordBoxActuel.Password, passwordBoxFutur1.Password, passwordBoxFutur2.Password);
                if (resultat == 0)
                {
                    message = "Mot de passe modifié avec succès !";
                }
                if (resultat == 1)
                {
                    message = "Mot de passe actuel erroné !";
                }
                if (resultat == 2)
                {
                    message = "Nouveau mot de passe différent du mot de passe de confirmation";
                }
                passwordBoxActuel.Password = "";
                passwordBoxFutur1.Password = "";
                passwordBoxFutur2.Password = "";

                MessageBox.Show(message);
            }
        }
    }
}
